﻿using BoriPeter.NutritionManager.FatSecret;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.Odbc;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BoriPeter.NutritionManager.WebFormsClient
{
    public partial class AddFromFatSecret : System.Web.UI.Page
    {
        protected async void Page_Load(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                InformationLabel.Text = "Invalid ID";
            }else
            {
                int id = 0;
                Int32.TryParse(Request.QueryString["ID"], out id);
                var message = new FoodGetMessage(id);
                var response = await message.SendAsync();

                var serving = response.Servings[0];

                var amount = serving.MetricServingAmount;
                if (amount == 0) amount = 100;

                var calories = (serving.Calories / amount) * 100;
                var carbohydrate = (serving.Carbohydrate / amount) * 100;
                var protein = (serving.Protein / amount) * 100;
                var fat = (serving.Fat / amount) * 100;
                var cholesterol = (serving.Cholesterol / amount) * 100;
                var sugar = (serving.Sugar / amount) * 100;
                var vitaminA = (serving.VitaminA / amount) * 900 * 100;
                var vitaminC = (serving.VitaminC / amount) * 60 * 100;
                var calcium = (serving.Calcium / amount) * 1000 * 100;
                var iron = (serving.Iron / amount) * 18 * 100;

                MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
                MySqlCommand cmd = new MySqlCommand("", con);
                cmd.CommandText = "INSERT INTO Foods (FoodName, Calories, Carbohydrate, Protein, Fat, Cholesterol, Sugar, VitaminA, VitaminC, Calcium, Iron) "
                    + "VALUES (@FoodName, @Calories, @Carbohydrate, @Protein, @Fat, @Cholesterol, @Sugar, @VitaminA, @VitaminC, @Calcium, @Iron)";

                cmd.Parameters.AddWithValue("@FoodName", response.FoodName);
                cmd.Parameters.AddWithValue("@Calories", calories);
                cmd.Parameters.AddWithValue("@Carbohydrate", carbohydrate);
                cmd.Parameters.AddWithValue("@Protein", protein);
                cmd.Parameters.AddWithValue("@Fat", fat);
                cmd.Parameters.AddWithValue("@Cholesterol", cholesterol);
                cmd.Parameters.AddWithValue("@Sugar", sugar);
                cmd.Parameters.AddWithValue("@VitaminA", vitaminA);
                cmd.Parameters.AddWithValue("@VitaminC", vitaminC);
                cmd.Parameters.AddWithValue("@Calcium", calcium);
                cmd.Parameters.AddWithValue("@Iron", iron);
                
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

                FoodName.Text = response.FoodName;
                Calories.Text = calories.ToString("0.0")+" kcal";
                Carbohydrate.Text = carbohydrate.ToString("0.0") + " g";
                Protein.Text = protein.ToString("0.0") + " g";
                Fat.Text = fat.ToString("0.0") + " g";
                Cholesterol.Text = cholesterol.ToString("0.0") + " g";
                Sugar.Text = sugar.ToString("0.0") + " g";
                VitaminA.Text = vitaminA.ToString("0.0") + " ug";
                VitaminC.Text = vitaminC.ToString("0.0") + " mg";
                Calcium.Text = calcium.ToString("0.0") + " mg";
                Iron.Text = iron.ToString("0.0") + " mg";
            }

        }
    }
}